package com.cqie.secondhandtransaction.service.Impl;

import com.cqie.secondhandtransaction.DTO.AccessDTO;
import com.cqie.secondhandtransaction.DTO.UserDetailsDTO;
import com.cqie.secondhandtransaction.entity.AccessInfo;
import com.cqie.secondhandtransaction.enums.RoleEnum;
import com.cqie.secondhandtransaction.exception.BusinessException;
import com.cqie.secondhandtransaction.mapper.AccessInfoMapper;
import com.cqie.secondhandtransaction.service.AccessInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.secondhandtransaction.service.file.AliYunService;
import com.cqie.secondhandtransaction.utils.WebSecurityUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  评价服务实现类
 * </p>
 *
 * @author gdy
 * @since 2025-02-10
 */
@Service
public class AccessInfoServiceImpl extends ServiceImpl<AccessInfoMapper, AccessInfo> implements AccessInfoService {

    @Autowired
    private AccessInfoMapper accessInfoMapper;
    @Autowired
    private AliYunService ossService;

    /**
     * 添加评论
     * @param dto
     */
    @Override
    public void addAccess(AccessDTO dto, List<MultipartFile> pictures) {
        UserDetailsDTO user = WebSecurityUtil.getCurrentUser();
        AccessInfo accessInfo = new AccessInfo();
        //评分相关
        accessInfo.setDescScore(dto.getDescScore());
        accessInfo.setServiceScore(dto.getServiceScore());
        accessInfo.setQualityScore(dto.getQualityScore());

        accessInfo.setDescription(dto.getDesc());
        accessInfo.setProductId(dto.getProductId());
        accessInfo.setUserId(user.getId());

        //照片上传oss，并将url存入数据库
        if (pictures != null && !pictures.isEmpty()) {
            try {
                List<String> url = ossService.uploadFiles(pictures);
                accessInfo.setPicture(CollectionUtils.isNotEmpty(url) ? String.join(",", url) : "");
            } catch (IOException e) {
                throw new BusinessException("图片上传失败！");
            }
        }

        accessInfoMapper.insert(accessInfo);
    }

    /**
     * 删除评价
     * @param id
     */
    @Override
    public void deleteAccess(Integer id) {
        AccessInfo accessInfo = accessInfoMapper.selectById(id);
        if(Objects.isNull(accessInfo)){
            throw new BusinessException("评论不存在！");
        }
        UserDetailsDTO user = Objects.nonNull(WebSecurityUtil.getCurrentUser()) ? WebSecurityUtil.getCurrentUser() : new UserDetailsDTO();

        if (Objects.equals(user.getRole(), RoleEnum.ADMIN.getRole())){
            //管理员可以直接删除
            if (Objects.nonNull(accessInfo.getPicture())) {
                ossService.deleteFilesByUrl(Arrays.asList(accessInfo.getPicture().split(",")));
            }
            accessInfoMapper.deleteById(id);
        }else if (Objects.equals(accessInfo.getUserId(), user.getId())){
            //用户只能删除自己的评论
            if (Objects.nonNull(accessInfo.getPicture())) {
                ossService.deleteFilesByUrl(Arrays.asList(accessInfo.getPicture().split(",")));
            }
            accessInfoMapper.deleteById(id);
        }else {
            throw new BusinessException("无法删除！原因：只能删除自己的评论");
        }
    }
}
